﻿using Dapper;
using SIG.DAL.Dapper.Model;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIG.DAL.Dapper.Repositories
{
    public class CarouselRepository
    {
        private DbConnection _connection;

        public Carousel GetById(int id)
        {
            Carousel result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Get<Carousel>(id);
            }
            return result;
        }
        public int? Create(Carousel model)
        {
            int? result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Insert(model);
            }
            return result;
        }

        public int Update(Carousel model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Update(model);
            }
            return result;
        }
        public int Delete(Carousel model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete(model);
            }
            return result;
        }

        public int Delete(int id)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete<Carousel>(id);
            }
            return result;
        }

        public IEnumerable<Carousel> GetActiveItems()
        {
            IEnumerable<Carousel> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<Carousel>(new { Active = true });
            }
            return result;
        }

        public IEnumerable<Carousel> GetActiveItems(int parentId)
        {
            IEnumerable<Carousel> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<Carousel>("where Active = 1 and PositionId = @PositionId", new { PositionId = parentId });
            }
            return result;
        }

        public IEnumerable<Carousel> GetItems(int parentId)
        {
            IEnumerable<Carousel> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<Carousel>("where PositionId = @PositionId", new { PositionId = parentId });
            }
            return result;
        }

        /// <summary>
        /// 获取所有文章分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="parentId"></param>
        /// <param name="keyword"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public IEnumerable<Carousel> GetListPagedItems(int pageIndex, int pageSize, int parentId,string keyword, out int totalCount)
        {
                           
            IEnumerable<Carousel> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                keyword = !string.IsNullOrEmpty(keyword) ? keyword.Replace("[", "[[]").Replace("%", "[%]") : keyword;
                string likeKeyword = "%" + keyword + "%";

                var sql = "where 1=1 ";

                if (!string.IsNullOrEmpty(keyword))
                {
                    sql = sql + " and Title like @Keyword ";
                }
                if (parentId > 0)
                {
                    sql = sql + " and PositionId = @PositionId ";
                }

                totalCount = _connection.RecordCount<Carousel>(sql,
                    new { PositionId = parentId , Keyword = likeKeyword });
                result = _connection.GetListPaged<Carousel>(pageIndex, pageSize,
                    sql, "Importance desc",
                    new { PositionId = parentId, Keyword = likeKeyword});
            }
            return result;
        }
        
    }

}
